package com.junmo.fanbizhidemo.demoview;

import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;

import com.bigkoo.pickerview.TimePickerView;
import com.junmo.fanbizhidemo.R;
import com.junmo.fanbizhidemo.back.MyTimeButton;
import com.junmo.fanbizhidemo.view.widget.CustomDatePicker;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 时间 或者三级联动
 */
public class DateActivity extends AppCompatActivity {

    @BindView(R.id.button_time)
    Button buttonTime;
    @BindView(R.id.button_txt_date)
    TextView buttonTxtDate;

    TimePickerView pvTime;
    @BindView(R.id.button_time2)
    Button buttonTime2;
    @BindView(R.id.button_time3)
    Button buttonTime3;
    @BindView(R.id.button_time4)
    Button buttonTime4;


    int mYear, mMonth, mDay;
    final int DATE_DIALOG = 1;


    private CustomDatePicker customDatePicker1, customDatePicker2;
    private String now;//当前时间

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_date);
        ButterKnife.bind(this);
        init();
    }

    private void init() {
        //第一种特效
        pvTime = MyTimeButton.setMyTimeButton(this);
        //第二种
        final Calendar ca = Calendar.getInstance();
        mYear = ca.get(Calendar.YEAR);
        mMonth = ca.get(Calendar.MONTH);
        mDay = ca.get(Calendar.DAY_OF_MONTH);
        //第三种
        initDatePicker();

    }



    private void initDatePicker() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.CHINA);
        now = sdf.format(new Date());
//        buttonTime3.setText(now.split(" ")[0]);
        buttonTime4.setText(now);

        customDatePicker1 = new CustomDatePicker(this, new CustomDatePicker.ResultHandler() {
            @Override
            public void handle(String time) { // 回调接口，获得选中的时间
                buttonTime3.setText(time.split(" ")[0]);
            }
        }, "2010-01-01 00:00", "2099-01-01 00:00"); // 初始化日期格式请用：yyyy-MM-dd HH:mm，否则不能正常运行
        customDatePicker1.showSpecificTime(false); // 不显示时和分
        customDatePicker1.setIsLoop(false); // 不允许循环滚动

        customDatePicker2 = new CustomDatePicker(this, new CustomDatePicker.ResultHandler() {
            @Override
            public void handle(String time) { // 回调接口，获得选中的时间
                buttonTime4.setText(time);
            }
        }, "2010-01-01 00:00", now); // 初始化日期格式请用：yyyy-MM-dd HH:mm，否则不能正常运行
        customDatePicker2.showSpecificTime(true); // 显示时和分
        customDatePicker2.setIsLoop(true); // 允许循环滚动
    }

    @OnClick({R.id.button_time, R.id.button_time2, R.id.button_time3, R.id.button_time4})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.button_time:
                if (pvTime != null) {
                    pvTime.show();
                }
                MyTimeButton.setInterfaceTimeView(new MyTimeButton.InterfaceTimeView() {
                    @Override
                    public void setView(View view, String time) {
                        buttonTxtDate.setText(time);
                    }
                });
                break;
            case R.id.button_time2:
                showDialog(DATE_DIALOG);
                break;
            case R.id.button_time3:
                // 日期格式为yyyy-MM-dd
                customDatePicker1.show(now.split(" ")[0]);
                break;
            case R.id.button_time4:
                // 日期格式为yyyy-MM-dd HH:mm
                customDatePicker2.show(buttonTime4.getText().toString());
                break;

        }
    }

    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
            case DATE_DIALOG:
                return new DatePickerDialog(this, mdateListener, mYear, mMonth, mDay);
        }
        return null;
    }

    /**
     * 设置日期 利用StringBuffer追加
     */
    public void display() {
        buttonTxtDate.setText(new StringBuffer().append(mMonth + 1).append("-").append(mDay).append("-").append(mYear).append(" "));
    }

    private DatePickerDialog.OnDateSetListener mdateListener = new DatePickerDialog.OnDateSetListener() {

        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear,
                              int dayOfMonth) {
            mYear = year;
            mMonth = monthOfYear;
            mDay = dayOfMonth;
            display();
        }
    };
}
